二叉树的遍历Java实现

使用Stack 遍历树而不递归。下面是使用堆栈遍历二叉树的算法。

1)创建一个空栈S.
2)以root身份初始化当前节点
3)将当前节点推送到S,并设置current = current-> left,直到current为NULL
4)如果current为NULL,堆栈不为空 
     a)从堆栈中弹出顶部元素。
     b)打印弹出的元素,设置current = popped_item-> right 
     c)转到步骤35)如果current为NULL,堆栈为空,遍历结束。

让我们考虑下面树的遍历例如

           1
          / \
        2    3
       / \
     4     5

步骤1 创建一个空堆栈:S = NULL

步骤2 初始化当前节点作为根节点:current  - > 1

步骤3 将当前节点入栈,并设置current = current-> left,直到current为NULL
     current - > 1
     Push 1:堆栈S  - > 1
     current - > 2
     push 2Stack S  - > 21
     current - > 4
     push 4Stack S  - > 421
     current = NULL

步骤44从S中弹出
     a)Pop 4Stack S  - > 21
     b)打印“4”
     c)current = NULL / *右边的4 * /并转到步骤3
因为current是NULL,第3步不做任何事情。 

步骤4 再次弹出。
     a)Pop 2Stack S  - > 1
     b)打印“2”
     c)current - > 5 / *右侧的2 * /并转到步骤3

步骤3 Push 5栈并使当前为NULL
     堆栈S  - > 51
     current = NULL

步骤4从S中弹出
     a)Pop 5Stack S  - > 1
     b)打印“5”
     c)current = NULL / *右边5 * /并转到步骤3
因为current是NULL,第3步不做任何事情

步骤4再次弹出。
     a)Pop 1:栈S  - > NULL
     b)打印“1”
     c)current - > 3 / *右边5 * /  

步骤3 Push 3到堆栈,并使当前为NULL
     堆栈S  - > 3
     current = NULL

步骤4从S中弹出
     a)Pop 3:栈S  - > NULL
     b)打印“3”
     c)current = NULL / *右边的3 * /  

遍历现在完成,因为堆栈S为空且当前为NULL

代码


// non-recursive java program for inorder traversal

/* importing the necessary class */
import java.util.Stack;

/* Class containing left and right child of current 
 node and key value*/
class Node {

    int data;
    Node left, right;

    public Node(int item) {
        data = item;
        left = right = null;
    }
}

/* Class to print the inorder traversal */
class BinaryTree {

    Node root;

    void inorder() {
        if (root == null) {
            return;
        }

        //keep the nodes in the path that are waiting to be visited
        Stack<Node> stack = new Stack<Node>();
        Node node = root;

        //first node to be visited will be the left one
        while (node != null) {
            stack.push(node);
            node = node.left;
        }

        // traverse the tree
        while (stack.size() > 0) {

            // visit the top node
            node = stack.pop();
            System.out.print(node.data + " ");
            if (node.right != null) {
                node = node.right;

                // the next node to be visited is the leftmost
                while (node != null) {
                    stack.push(node);
                    node = node.left;
                }
            }
        }
    }

    public static void main(String args[]) {

        /* creating a binary tree and entering 
         the nodes */
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
        tree.inorder();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值